/*
 * RemoteObjectProxy.java    0.0.1    Nov 3, 2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.net.ril;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.mentalsmash.tazio.net.ril.exceptions.NoSuchRemoteObjectException;


/**
 * This class is responsible for intercepting method invocations made on
 * {@link RemoteObject} instances and dispatch them to a local instance (if the
 * object identified by their OID is present in the local registry) or a remote
 * one. It implements the {@link InvocationHandler} interface to be used in a
 * {@link Proxy} and the {@link RemoteObject} one to provide an implementation
 * of it for any instance registered for remote method invocation.
 * 
 * @version 0.0.1 Nov 3, 2009
 * @author Andrea Sorbini <as@mentalsmash.org>
 * 
 */
public interface RemoteObjectProxy extends InvocationHandler,Serializable {
	/**
	 * Sets the registry instance to be used by this proxy for local
	 * invocations.
	 * 
	 * @param registry
	 */
	public void setRegistry(RemoteObjectsRegistry registry);
	
	/**
	 * Sets the dispatcher instance to be used by this proxy for remote
	 * invocations.
	 * 
	 * @param dispatcher
	 */
	public void setDispatcher(RemoteInvocationDispatcher dispatcher);

	/**
	 * Implementation of
	 * {@link InvocationHandler#invoke(Object, Method, Object[])}.<br/>
	 * It uses {@link RemoteObjectProxy#invoke(RemoteMethod, Object[])} to
	 * actually carry out the method invocation.
	 */
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
	
	/**
	 * Invokes a method with the supplied arguments on the instance handled by
	 * this proxy. The invocation is handled locally if the instance identified
	 * by this proxy's OID is present in the local registry. Otherwise it is
	 * remotely dispatched using the {@link RemoteInvocationDispatcher}.
	 * 
	 * @param method
	 *            the method to invoke
	 * @param args
	 *            the arguments to invoke the method with
	 * @return the result of the method invocation
	 * @throws Exception
	 *             if any was raised during method's invocation
	 */
	public Object invoke(RemoteMethod method, Object[] args) throws Exception;

	/**
	 * Forces the invocation of the specified method on the local machine.
	 * Throws an exception if no instance is registered on the local registry for 
	 * the proxy's OID.
	 * 
	 * @param method
	 * @param args
	 * @return the outcome of the invocation.
	 */
	public Object invokeLocally(RemoteMethod method, Serializable[] args) throws NoSuchRemoteObjectException;

}
